<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        *{padding: 0; margin: 0;}
        div {
            text-align: center;
            width: 800px;
            margin: 50px auto;
        }
        #pic{
            width: 100%;
            height: 100%;
        }
    </style>


</head>
<body>
    <div>
        <img src="./images/pngs/1.png" id="pic" alt="轮播图">
        <button id="btnStart">开始轮播</button>
        <button id="btnStop">停止轮播</button>
    </div>

    <script type="text/javascript">
        let index = 1; //保存当前的到图片的编号
        let img = document.getElementById("pic");
        let clear;
        document.getElementById("btnStart").onclick=function () {
            clear = setInterval(slideshow, 2000);//每隔2s 调用一次
        }
        document.getElementById("btnStop").onclick=function () {
            clearInterval(clear);//调用setInterval()的清理方法
        }
        //控制轮播的函数
        function slideshow() {
            index++;
            if (index === 5) {
                index = 1;
            }
            img.src = './images/pngs/' + index + '.png';
        }
    </script>
</body>
</html>
